package scau.zhong.config;

import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import scau.zhong.common.user.UserServiceException;
import scau.zhong.common.user.auth.RsaUtils;

import javax.annotation.PostConstruct;
import java.security.PrivateKey;
import java.security.PublicKey;


/**
 * 密钥配置类
 */
@ConfigurationProperties("rsa.key")
@Data
public class RsaKeyProperties {

    //私钥路径
    private String priKeyFile;
    //公钥路径
    private String pubKeyFile;

    private PublicKey publicKey;

    private PrivateKey privateKey;

    //在IOC容器注入后才能开始构造
    @PostConstruct
    public void createRsaKey() throws UserServiceException {
        try {
            publicKey = RsaUtils.getPublicKey(pubKeyFile);
            privateKey = RsaUtils.getPrivateKey(priKeyFile);
        } catch (Exception e) {
            throw new UserServiceException("密钥获取发生错误" + e.getMessage());
        }
    }
}
